<template>
  <div class="login">
       
       <div class="login-box">
            <!--头像-->
            <div class="faeimg">
                 <img src="../assets/logo.png" alt="">
            </div>
            <!-- 数据表单区 -->
            <el-form :model="ruleForm"  :rules="loginrules" ref="loginruleForm" label-width="80px" >
                <el-form-item label="用户名" prop="name">
                    <el-input type="text" prefix-icon="el-icon-user-solid" v-model="ruleForm.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="pass">
                    <el-input prefix-icon="iconfont icon-mima" type="password" v-model="ruleForm.pass"  show-password></el-input>
                </el-form-item>
                <el-form-item class="btns">
                    <el-button type="primary" @click="loginsubmitForm('loginruleForm')">登录</el-button>
                    <el-button type="danger" @click="loginresetForm('loginruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
       </div>
  </div>
</template>

<script>
export default {
    data(){
        return {
            // 数据双向绑定
            ruleForm: {
                name: 'admin',
                pass:'123456'
            },
            //表单校验规则 
            loginrules: {
                name: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                    ],
                pass: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 6, max: 8, message: '长度在 6到 8 个字符', trigger: 'blur' }
                    ],
            }
        }
    },
    methods:{
        // 重置表单数据
        loginresetForm(formName) {
            this.$refs[formName].resetFields();
        },
        // 表单预验证
        loginsubmitForm(formName) {
            this.$refs[formName].validate(async (valid) => {
            if (valid) {
                // 表单预验证成功发起登录请求
                const {data:res} = await this.$http.post('login',{
                    "username":this.ruleForm.name,
                    "password":this.ruleForm.pass
                })
                if(res.meta.status!==200)
                {this.loginresetForm(formName)
                    return this.$message.error("登录失败")}
                else {
                    window.sessionStorage.setItem('token',res.data.token)
                    this.$message.success("登录成功")
                    this.$router.push('/home')
                    return
                 } 
            }
        });
        }
    }
}
</script>

<style scoped>
.login{
    position: absolute;
    width: 100%;
    height: 100%;
    background-color: rgb(57, 43, 107);
}
.faeimg{
    position: absolute;
    top: -100px;
    left: 100px;
    width: 200px;
    height: 200px;
    border-radius: 50%;
    background-color: #eee;
    padding: 4px;
    border: 4px solid #FFF;
}
.faeimg img{
    width: 200px;
    height: 200px;
    border-radius: 50%;
}
.login-box{
    position: relative;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    width: 400px;
    height: 400px;
    background-color: #eee;
}
.el-form{
   text-align: center;
   width: 360px;
   margin-top: 40px;
   margin-left: -20px;
   box-sizing: border-box;
   position: absolute;
   left: 50%;
   top: 50%;
   transform: translate(-50%,-50%);
}
.el-form-item{  
    width: 100%;
    height: 40px;
    line-height: 40px;
}
.btns{
    display: flex;
    justify-content: flex-end;
}
</style>